﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Windows.Controls;
using Microsoft.LightSwitch.Threading;

namespace LightSwitchApplication
{
    public partial class RolesManager
    {
        System.Windows.Controls.TextBox m_applicationName = null;
        System.Windows.Controls.TextBox m_description = null;
        aspnet_Application newSite = null;

        partial void RolesManager_InitializeDataWorkspace(global::System.Collections.Generic.List<global::Microsoft.LightSwitch.IDataService> saveChangesTo)
        {
            // Write your code here.
            //this.aspnet_RoleProperty = new aspnet_Role();
        }

        partial void RolesManager_Saved()
        {
            // Write your code here.
            this.Close(false);
            //Application.Current.ShowDefaultScreen(this.aspnet_RoleProperty);
        }

        partial void RolesManager_Activated()
        {
            // Write your code here.
        }

        void RolesManager_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            
            Console.Write("CIAO");
        }

        partial void aspnet_ApplicationListAddAndEditNew_CanExecute(ref bool result)
        {
            // Write your code here.
            result = this.aspnet_Applications.CanAddNew;
        }

        partial void aspnet_ApplicationListAddAndEditNew_Execute()
        {
            // Write your code here.
            newSite = new aspnet_Application();
            this.aspnet_Applications.SelectedItem = newSite;
            this.OpenModalWindow("NewSiteModal");
        }

        partial void Chiudi_Execute()
        {
            // Write your code here.
            this.CloseModalWindow("NewSiteModal");
        }

        partial void Aggiungi_Execute()
        {
            Dispatchers.Main.BeginInvoke(() =>
            {
                newSite.ApplicationName = m_applicationName.Text;
                newSite.Description = m_description.Text;

                newSite.LoweredApplicationName = newSite.ApplicationName.ToLower();

                newSite.Details.EntitySet.AddNew();            
            });
        }

        partial void RolesManager_Created()
        {
            // Write your code here.
            Dispatchers.Main.BeginInvoke(() =>
            {
                this.FindControl("txtNewSiteCode").ControlAvailable += txtNewSiteCode_Available;
                this.FindControl("txtNewSiteDescription").ControlAvailable += txtNewSiteDescription_Available;
            });
        }

        private void txtNewSiteCode_Available(object sender, ControlAvailableEventArgs e)
        {
            m_applicationName = (TextBox)e.Control;
            this.FindControl("txtNewSiteCode").ControlAvailable -= new EventHandler<ControlAvailableEventArgs>(txtNewSiteCode_Available);
        }

        private void txtNewSiteDescription_Available(object sender, ControlAvailableEventArgs e)
        {
            m_description = (TextBox)e.Control;
            this.FindControl("txtNewSiteDescription").ControlAvailable -= new EventHandler<ControlAvailableEventArgs>(txtNewSiteDescription_Available);
        }
    }
}